package com.bingxue.edu.management.education.service;

import com.bingxue.edu.common.enums.JoinTypeEnum;
import com.bingxue.edu.common.enums.StudentClassEnum;
import com.bingxue.edu.framework.excel.model.ClassStudentExportModel;
import com.bingxue.edu.framework.mybatis.base.BaseService;
import com.bingxue.edu.management.education.mapper.ClassStudentMapper;
import com.bingxue.edu.management.education.model.entity.ClassStudent;
import com.bingxue.edu.management.education.model.resp.ClassStudentResp;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.bingxue.edu.management.education.model.entity.table.ClassStudentTableDef.CLASS_STUDENT;
import static com.bingxue.edu.management.education.model.entity.table.EnrollmentTableDef.ENROLLMENT;
import static com.bingxue.edu.management.education.model.entity.table.StudentTableDef.STUDENT;

/**
 * 班级学员实现类
 * @author zq
 * @since 2024/08/22
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ClassStudentService extends BaseService<ClassStudentMapper, ClassStudent> {

    /**
     * 更新班级学生状态
     * @param studentId 学生id
     * @param classId 班级id
     * @param status 学生行课状态
     */
    public void updateStudentClassStatus(String studentId, String classId, StudentClassEnum status) {
        this.updateChain().set(CLASS_STUDENT.STUDENT_CLASS_STATUS, status)
                .where(CLASS_STUDENT.STUDENT_ID.eq(studentId))
                    .and(CLASS_STUDENT.CLASS_ID.eq(classId))
                    .and(CLASS_STUDENT.STUDENT_CLASS_STATUS.eq(StudentClassEnum.NORMAL))
                .update();
    }

    /**
     * 创建班级学生信息
     *
     * @param studentId    学生id
     * @param classId      班级id
     * @param enrollmentId 报名id
     * @param joinType     加入方式
     */
    public void createClassStudent(String studentId, String classId, String enrollmentId, JoinTypeEnum joinType) {
        ClassStudent classStudent = new ClassStudent();
        classStudent.setEnrollmentId(enrollmentId);
        classStudent.setClassId(classId);
        classStudent.setStudentId(studentId);
        classStudent.setStudentJoinType(joinType);
        classStudent.setStudentClassStatus(StudentClassEnum.NORMAL);
        this.save(classStudent);
    }

    /**
     * 根据学生id查询班级信息
     */
    public List<ClassStudent> getListByClassId(String classId, StudentClassEnum studentClassEnum) {
        return queryChain()
                .where(CLASS_STUDENT.CLASS_ID.eq(classId))
                .and(CLASS_STUDENT.STUDENT_CLASS_STATUS.eq(studentClassEnum))
                .orderBy(CLASS_STUDENT.STUDENT_JOIN_TYPE.asc())
                .list();
    }

    /**
     * 查询班级学生信息
     */
    public List<ClassStudentResp> getStudentListByClassId(String classId) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(
                        CLASS_STUDENT.DEFAULT_COLUMNS,
                        ENROLLMENT.DEFAULT_COLUMNS,
                        STUDENT.DEFAULT_COLUMNS
                )
                .from(CLASS_STUDENT)
                .leftJoin(STUDENT).on(STUDENT.ID.eq(CLASS_STUDENT.STUDENT_ID))
                .leftJoin(ENROLLMENT).on(ENROLLMENT.ID.eq(CLASS_STUDENT.ENROLLMENT_ID))
                .where(CLASS_STUDENT.CLASS_ID.eq(classId))
                    .and(CLASS_STUDENT.STUDENT_CLASS_STATUS.eq(StudentClassEnum.NORMAL))
                .orderBy(CLASS_STUDENT.STUDENT_JOIN_TYPE.asc());
        return listAs(queryWrapper, ClassStudentResp.class);
    }

    /**
     * 查询excel导出数据
     */
    public List<ClassStudentExportModel> getStudentExportModelListByClassId(String classId) {

        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(
                        CLASS_STUDENT.ENROLLMENT_ID.as(ClassStudentExportModel::getEnrollmentId),
                        CLASS_STUDENT.STUDENT_ID.as(ClassStudentExportModel::getStudentId),
                        CLASS_STUDENT.STUDENT_JOIN_TYPE.as(ClassStudentExportModel::getStudentJoinType),
                        CLASS_STUDENT.STUDENT_CLASS_STATUS.as(ClassStudentExportModel::getStudentClassStatus),
                        CLASS_STUDENT.CLASS_ID.as(ClassStudentExportModel::getClassId),
                        CLASS_STUDENT.REMARK.as(ClassStudentExportModel::getRemark),
                        ENROLLMENT.ENROLL_LESSON_COUNT.as(ClassStudentExportModel::getEnrollLessonCount),
                        ENROLLMENT.CANCEL_LESSON_COUNT.as(ClassStudentExportModel::getCancelLessonCount),
                        ENROLLMENT.COMPLETED_LESSON_COUNT.as(ClassStudentExportModel::getCompletedLessonCount),
                        ENROLLMENT.REMAIN_CLASS_CHANGE_COUNT.as(ClassStudentExportModel::getRemainClassChangeCount),
                        ENROLLMENT.REMAIN_LESSON_CHANGE_COUNT.as(ClassStudentExportModel::getRemainLessonChangeCount),
                        STUDENT.NAME.as(ClassStudentExportModel::getStudentName),
                        STUDENT.GENDER.as(ClassStudentExportModel::getGender)
                )
                .from(CLASS_STUDENT)
                .leftJoin(STUDENT).on(STUDENT.ID.eq(CLASS_STUDENT.STUDENT_ID))
                .leftJoin(ENROLLMENT).on(ENROLLMENT.ID.eq(CLASS_STUDENT.ENROLLMENT_ID))
                .where(ENROLLMENT.CLASS_ID.eq(classId))
                .and(CLASS_STUDENT.STUDENT_CLASS_STATUS.eq(StudentClassEnum.NORMAL))
                .orderBy(CLASS_STUDENT.STUDENT_JOIN_TYPE.asc());
        return this.getMapper().selectListWithRelationsByQueryAs(queryWrapper, ClassStudentExportModel.class);
    }
}
